module Classes {

    export class BaseObject extends Laya.Sprite {
        public static loaded: boolean;
        public static config: any;

        public team: string;
        public type: string;

        protected blood: number;
        protected speed: number;
        public radius: number;

        protected body: Laya.Animation;

        protected action: string;

        constructor() {
            super();

            if (!BaseObject.loaded) {
                BaseObject.loaded = true;

                Laya.Animation.createFrames(["drop/drop_weapon.png"], "drop_weapon_fly");
                Laya.Animation.createFrames(["drop/drop_weapon.png"], "drop_weapon_hit");
                Laya.Animation.createFrames(["drop/drop_weapon.png"], "drop_weapon_crash");
                Laya.Animation.createFrames(["drop/drop_food.png"], "drop_food_fly");
                Laya.Animation.createFrames(["drop/drop_food.png"], "drop_food_hit");
                Laya.Animation.createFrames(["drop/drop_food.png"], "drop_food_crash");

                Laya.Animation.createFrames(["player/player_01.png", "player/player_02.png"], "player_fly");
                Laya.Animation.createFrames(["player/player_01.png", "player/player_02.png"], "player_hit");
                Laya.Animation.createFrames([
                    "player/crash_01.png", 
                    "player/crash_02.png",
                    "player/crash_03.png", 
                    "player/crash_04.png",
                ], "player_crash");

                Laya.Animation.createFrames(["enemy/big_enemy_01.png", "enemy/big_enemy_02.png"], "big_enemy_fly");
                Laya.Animation.createFrames(["enemy/big_enemy_03.png"], "big_enemy_hit");
                Laya.Animation.createFrames([
                    "enemy/big_enemy_04.png",
                    "enemy/big_enemy_05.png",
                    "enemy/big_enemy_06.png",
                    "enemy/big_enemy_07.png",
                    "enemy/big_enemy_08.png",
                    "enemy/big_enemy_09.png",
                ], "big_enemy_crash");

                Laya.Animation.createFrames(["enemy/mid_enemy_01.png"], "mid_enemy_fly");
                Laya.Animation.createFrames(["enemy/mid_enemy_02.png"], "mid_enemy_hit");
                Laya.Animation.createFrames([
                    "enemy/mid_enemy_03.png",
                    "enemy/mid_enemy_04.png",
                    "enemy/mid_enemy_05.png",
                    "enemy/mid_enemy_06.png"
                ], "mid_enemy_crash");

                Laya.Animation.createFrames(["enemy/small_enemy_01.png"], "small_enemy_fly");
                Laya.Animation.createFrames(["enemy/small_enemy_01.png"], "small_enemy_hit");
                Laya.Animation.createFrames([
                    "enemy/small_enemy_02.png",
                    "enemy/small_enemy_03.png",
                    "enemy/small_enemy_04.png",
                    "enemy/small_enemy_05.png"
                ], "small_enemy_crash");

                Laya.Animation.createFrames(["bullet/bullet_01.png"], "bullet_fly");
                Laya.Animation.createFrames(["bullet/bullet_01.png"], "bullet_hit");
                Laya.Animation.createFrames(["bullet/bullet_01.png"], "bullet_crash");

                BaseObject.config = Laya.loader.getRes("res/config.json");
            }
        }

        public config(type: string, team: string): void {
            
            this.type = type;
            this.team = team || "player";

            if (BaseObject.config[this.type]) {
                this.blood = BaseObject.config[this.type].blood;
                this.speed = BaseObject.config[this.type].speed;
                this.radius = BaseObject.config[this.type].radius;
            }

            if (!this.body) {
                this.body = new Laya.Animation();
                this.addChild(this.body);
            }

            this.playAction("fly");
            this.action = "fly";

            //显示碰撞区域
            // this.graphics.clear();
            // this.graphics.drawCircle(0, 0, this.radius, null, "#ff0000");
        }

        public isAlive(): boolean {
            return (this.blood > 0 && this.visible);
        }

        public looseBlood(num: number): void {
            this.blood -= num;
            if (this.blood <= 0) {
                this.action = "down";
                this.playAction("crash");
            } else if (num > 0) {
                this.action = "hit";
                this.playAction("hit");
            } else if (num < 0) {
                // Maybe Play AddBlood Action
            }
        }

        protected playAction(name: string): void {
            let action: string = this.type + "_" + name;
            this.body.play(0, false, action);

            let bounds: Laya.Rectangle = this.body.getBounds();
            this.body.pos(-bounds.width/2, -bounds.height/2);
        }

        protected onComplete(): void {
            if (this.action === "down") {
                this.body.stop();
                this.visible = false;
                Laya.stage.removeSelf();
            } else {
                this.playAction("fly");
            }
        }

        public onLoop(): void {
            throw new Error("On Loop Must Be Implimented!");
        }

    }

}